<script>

if (!smaragdAdmin) var smaragdAdmin = {};

smaragdAdmin.selectedObject = {};

smaragdAdmin.editClick = function() {
	var editFields = readEditdFields();

	$.ajax({
	    type: "POST",
	    url: "/smaragd/admin/editproduct", 
	    data: editFields,
	    dataType: "text",
	    cache: false,
	    success: 
	         function(response) {
	    		alert("sikeres módositás");
	         },
	    error: 
	    	function (xhr, ajaxOptions, thrownError) {
	        	 alert("failer");
	        }
	 });
};

function readEditdFields() {
	var fields = {};

	fields.id = $("#admin-products-edit input[name='id']").val();
	fields.name = $("#admin-products-edit input[name='name']").val();
	fields.description = $("#admin-products-edit textarea[name='description']").val();
	fields.netto_price = $("#admin-products-edit input[name='netto_price']").val();
	fields.price = $("#admin-products-edit input[name='price']").val();
	fields.profit_key = $("#admin-products-edit input[name='profit_key']").val();
	fields.category_name = $("#admin-products-edit-categories").find(":selected").text();
	fields.category_id = $("#admin-products-edit-categories").find(":selected").data("id");
	
	return fields;
}

smaragdAdmin.productEditHandlers = function() {
	$(document).on("click", "#admin-products-edit-cancel", function () {
		$("#admin-products-edit").hide();
		$("#admin-products").show();
	});

	$(document).on("click", "#admin-newproduct", function () {
		$("#admin-products-edit").show();
		$("#admin-products").hide();
		$("#admin-products-edit-title").html("Termék felvétele");
		$("#admin-products-edit-ok").html("Hozzáad");
	});
	
	
	$(document).on("click", "#admin-products-edit-ok", function() {
		smaragdAdmin.editClick();
	});
	
	$(document).on("click", ".product-edit-link", function () {

		$("#admin-products-edit").show();
		$("#admin-products").hide();
		$("#admin-products-edit-title").html("Termék módosítása");
		$("#admin-products-edit-ok").html("Módosít");

		var row = $(this).parent().parent();
		
		var productId = $(this).data('id');
		var name = row.find(".name").text();
		var description = row.find(".description").text();
		var netto_price = row.find(".netto_price").text();
		var price = row.find(".price").text();
		var profit_key = row.find(".profit_key").text();
		var image = row.find(".image").text();
		var category = row.find(".category").text();
		
		smaragdAdmin.populateEditFields(productId, name, description, netto_price, price, profit_key, image, category);
	});
};

smaragdAdmin.categories.forEach(function(element) {
    //console.log(entry);
	$("#admin-products-edit-categories").append("<option data-id='" + element.category_id + "' data-name='" + element.name + "'>" + element.name + "</option>");
});
/*
for(element in smaragdAdmin.categories)
{
	debugger;
}
*/

smaragdAdmin.categories.forEach(function(entry) {
	console.log(entry.name);
});

smaragdAdmin.populateEditFields = function (productId, name, description, netto_price, price, profit_key, image, category) {

	smaragdAdmin.selectedObject = {
		id : productId,
		name : name,
		description : description,
		netto_price : netto_price,
		price : price,
		profit_key : profit_key,
		image : image,
		category : category 
	};
	
	$("#admin-products-edit input[name='name']").val(name);
	$("#admin-products-edit textarea[name='description']").val(description);
	$("#admin-products-edit input[name='netto_price']").val(netto_price);
	$("#admin-products-edit input[name='price']").val(price);
	$("#admin-products-edit input[name='profit_key']").val(profit_key);
	$("#admin-products-edit input[name='id']").val(productId);
	$("#admin-products-edit select.category").val(category);
	$("#admin-products-edit select option[data-name='" + category + "']").attr('selected', 'selected');
};

smaragdAdmin.productEditHandlers();

</script>